// @charset "utf-8";

define(function(require, exports, module) {
	var clientApi = {
		thsQuote: null,
		thsUtil: null,
		thsPassport: null,
		init: function() {
			var self = this;
			try {
				self.thsQuote = external.createObject('Quote');
				self.thsUtil = external.createObject('Util');
				self.thsPassport = external.createObject('Passport');

				self.getUserId();
				self.getUserName();
				self.getVersion();
			} catch (e) {

			}
		},
		userId: '',
		getUserId: function() {
			this.userId = this.thsPassport.get('userid');
		},
		userName: '',
		getUserName: function() {
			this.userName = this.thsPassport.get('account');
		},
		version: '',
		_version: '',
		getVersion: function() {
			var hxVersion = 'E020.00.00.0000';
			hxVersion = this.thsUtil.getHxVer();
			var hxVerArr = hxVersion.split('.');
			this.version = hxVerArr[0].substr(3) + hxVerArr[1] + hxVerArr[2] + hxVerArr[3];
			this._version = hxVerArr[0].substr(3) + '.' + hxVerArr[1] + '.' + hxVerArr[2];
		},
		openUrlWeb: function(url, type) {
			this.thsUtil.openURL(url, type);
		},
		serverTime: function(cut) {
			cut = cut || '';
			var timeStamp = this.thsQuote.getServerTime();
			var time = new Date(timeStamp * 1000);
			var y = time.getFullYear();
			var m = time.getMonth() + 1;
			var d = time.getDate();

			m = m < 10 ? '0' + m : m;
			d = d < 10 ? '0' + d : d;
			return y + cut + m + cut + d;
		},
		serverHour: function(cut) {
			cut = cut || '';
			var timeStamp = this.thsQuote.getServerTime();
			var time = new Date(timeStamp * 1000);
			var h = time.getHours();
			var m = time.getMinutes();

			h = h < 10 ? '0' + h : h;
			m = m < 10 ? '0' + m : m;
			return h + cut + m;
		},
		preMonthDate: function(cut) {
			cut = cut || '';
			var timeStamp = this.thsQuote.getServerTime();
			timeStamp = timeStamp * 1000 - 30 * 24 * 3600 * 1000;
			var preMonth = this.timeStamp(timeStamp, cut);
			return preMonth;
		},
		preDate: function(cut) {
			cut = cut || '';
			var timeStamp = this.thsQuote.getServerTime();
			timeStamp = timeStamp * 1000 - 1 * 24 * 3600 * 1000;
			var preDay = this.timeStamp(timeStamp, cut);
			return preDay;
		},
		timeStamp: function(timestamp, cut) {
			cut = cut || '';
			var time = new Date(timestamp);
			var y = time.getFullYear();
			var m = time.getMonth() + 1;
			var d = time.getDate();

			m = m < 10 ? '0' + m : m;
			d = d < 10 ? '0' + d : d;
			return y + cut + m + cut + d;
		},
		judgeIntime: function(arr) {
			var timeStamp = this.thsQuote.getServerTime();
			var time = new Date(timeStamp * 1000);
			var current = time.getHours() * 60 + time.getMinutes();
			var arrBegin = arr[0].split(':');
			var arrEnd = arr[1].split(':');
			var b = parseFloat(arrBegin[0], 10) * 60 + parseFloat(arrBegin[1], 10);
			var e = parseFloat(arrEnd[0], 10) * 60 + parseFloat(arrEnd[1], 10);
			if (current >= b && current <= e) {
				return true;
			} else {
				return false;
			}
		},
		getTradingDay: function(date) {
			var today = date || this.serverTime();
			var tradingDay = this.thsQuote.getSHTradeDate(today);
			return tradingDay;
		},

		getSelfStock: function() {
			var selfStockStr = this.thsPassport.selfStocks();
			var selfStock = eval(selfStockStr);
			return selfStock;
		},
		addSelfStock: function(stocks) {
			this.thsQuote.setSelfStock({
				mode: 'add',
				code: stocks
			});
		},

		getCurPageId: function() {
			return this.thsQuote.getHxCurPageID();
		},
		getCurPageName: function() {
			try {
				return this.thsQuote.getHxCurPageName();
			} catch (e) {
				return '';
			}
		},

		addCustomBlock: function(name, condition, stocks) {
			var blockId;
			try {
				blockId = this.thsUtil.addCustomBlock(name, condition);
				if (blockId) {
					this.thsUtil.addStockToCustomBlock(blockId, stocks);
					return blockId;
				}
			} catch (e) {
				return false;
			}
			return false;
		}
	};

	module.exports = clientApi;
});